/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cnd;

import java.io.*;
import jcifs.smb.*;

/**
 *
 * @author marten
 */
public class Download extends Thread {

    int state;
    SmbFile smb_file;
    long size;
    SmbFileInputStream in;
    byte[] b;
    int n;
    File location;
    FileOutputStream out;
    long skip;
    String url;
    File outputfile;

    public Download(String url, String outputfile) {
        preinit(url, new File(outputfile));
    }

    public Download(SearchResult sr_file, String outputfile) {
        preinit(sr_file.full_path, new File(outputfile));
    }

    public Download(String url, File outputfile) {
        preinit(url, outputfile);
    }

    public Download(SearchResult sr_file, File outputfile) {
        preinit(sr_file.full_path, outputfile);
    }
    private void preinit(String url, File outputfile) {
        this.url = url;
        this.outputfile = outputfile;
    }

    private boolean init(String url, File outputfile) {
        System.out.println("init");
        state = 0;
        skip = 0;
        location = outputfile;
        System.out.println("Outputfile: " + outputfile.getAbsolutePath());
        try {
            if (location.exists()) {
                System.err.println("a file already exists at location, will try " +
                        "to continue writing it");
                if (location.canWrite()) {
                    out = new FileOutputStream(location, true);
                    skip = location.length();
                    System.out.println("OutputStream created");
                    return initsmb(url);
                } else {
                    System.err.println("cannot write to file");
                    state = -1;
                    return false;
                }
            } else {
                if (location.createNewFile()) {
                    System.err.println("created a new file");
                    if (location.canWrite()) {
                        out = new FileOutputStream(location);
                        skip = 0;
                        System.out.println("OutputStream created");
                        return initsmb(url);
                    } else {
                        System.err.println("cannot write to file");
                        state = -1;
                        return false;
                    }
                } else {
                    System.err.println("new file can't be created");
                    state = -1;
                    return false;
                }
            }
        } catch (Exception e) {
            System.err.println(e);
            state = -1;
            return false;
        }
    }

    private boolean initsmb(String url) {
        try {
            try {
                smb_file = new SmbFile(url);
                smb_file.connect();
            }catch(Exception e){
                state = -1;
                return false;
            }
            size = smb_file.length();
            state = 1;
            System.out.println("SMBFile created");
            try {
                in = new SmbFileInputStream(smb_file);
                state = 2;

                System.out.println("InputStream Created");
                b = new byte[8192];
                n = 0;
                return true;
            } catch (Exception e) {
                in = null;
                System.err.println(e);
                state = -1;
                return false;
            }
        } catch (Exception e) {
            smb_file = null;
            System.err.println(e);
            state = -1;
            return false;
        }
    }

    @Override
    public void run() {
        if(init(url, outputfile)){
            if (state != -1) {
                System.out.println("downloading");
                try {
                    state = 3;
                    while ((n = in.read(b)) > 0) {
                        out.write(b);
                    }
                    state = 4;
                    System.out.println("done");
                } catch (Exception e) {
                    state = -1;
                    System.err.println(e);
                    yield();
                }
                state = 4;
            }
        }
    }

    /*public void startDownload() {
    start();
    }*/
    /*
    public void stopDownload() {
    }
     */
    public File getLocation() {
        return location;
    }

    public double getPartComplete() {
        //System.out.println("Done: " + location.length() + " || Size: " + size);
        return ((double) location.length()) / ((double) size);
    }

    public int getDownloadState() {
        return state;
    }
}
